const is_online = false
const common = {
    is_online: is_online,
    // 资源图片域名
    image_url: 'https://img.ameimeika.com/h5_images/',
    // 接口图片域名
    image_response: 'https://img.ameimeika.com/',
    // php接口请求域名
    base_url: is_online
        ? 'https://app.ameimeika.com/'
        : 'http://test.act.ameimeika.com/',
    // java接口域名
    java_base_url: is_online
        ? 'https://openapi.ameimeika.com/'
        : 'https://mpingtest.ameimeika.com/',
    // 预上线
    // : 'https://app2.ameimeika.com/',
    // 请求方法
    fly_post (path, data = {}, callback_success, isJava) {
        /**
         * 网络请求方法
         * path:请求地址
         * data:请求参数
         * callback_success:请求成功回调，用参数接收返回值
         */
        let Fly = require('flyio/dist/npm/wx')
        let fly = new Fly()
        let that = this
        fly.config = {
            method: 'POST', //请求方法， GET 、POST ...
            headers: {
                'Authorization': '',
                'appVersion': '1.0',
                'appSystem': 'minapp',
                'content-type': 'application/json'
            },
            // 请求头
            baseURL: !isJava ? common.base_url : common.java_base_url, //请求基地址
            //是否自动将Content-Type为“application/json”的响应数据转化为JSON对象，默认为true
            parseJson: true,
            timeout: '10000' //超时时间
        }
        // 请求拦截器
        fly.interceptors.request.use(request => {
            request.timeout = 30000
            if (wx.getStorageSync('user_token')) {
                //检查本地缓存是否有token存在没有则重新获取
                request.headers = {
                    //设置请求头
                    'Authorization': 'Bearer ' + wx.getStorageSync('user_token'),
                    'content-type': 'application/json',
                }
                return request
            }
        })
        // 响应拦截器
        fly.interceptors.response.use(response => {
            //只将请求结果的data字段返回
            let res = response.data
            if (!is_online) {
                //   测试服打印
                console.log(res)
            }
            switch (res.status_code - 0) {
                case 1017:
                    wx.showToast({
                        title: res.message, //提示的内容,
                        icon: 'none', //图标,
                        duration: 1500, //延迟时间,
                        mask: true, //显示透明蒙层，防止触摸穿透,
                        success: res => {
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: '/pages/index/main'
                                })
                            }, 1500)
                        }
                    })
                    break           

                default:
                    break
            }
        })
        fly
            .post(path, data, isJava)
            .then(callback_success)
            .catch(err => {
                wx.hideLoading()
                console.log(err)
                wx.showToast({
                    title: '信号弱。。换个地方试试',
                    icon: 'none',
                    duration: 2000,
                    mask: false
                })
            })
    },
    $log (data) {
        if (!is_online) {
            console.log(data)
        }
    },
    mmk_Loading (e, msg, time = 1500, mask = false) {
        switch (e) {
            case 0:
                wx.showLoading({
                    title: '加载中',
                    mask: true
                })
                break
            case 1:
                wx.hideLoading()
                break
            case 2:
                wx.showToast({
                    title: msg,
                    icon: 'none',
                    duration: time,
                    mask: mask
                })
                break
        }
    },
    mmk_toast ({ type = 0, msg, time = 1500, mask = false }) {
        switch (type) {
            case 0:
                wx.showLoading({
                    title: '加载中',
                    mask: true
                })
                break
            case 1:
                wx.showToast({
                    title: msg,
                    icon: 'none',
                    duration: time,
                    mask: mask
                })
                break
        }
    },
  
    //轮播图预览方法
    tapBigImg (banners, index) {
        let arr = []
        banners.forEach(el => {
            el = 'https://img.ameimeika.com/' + el
            arr.push(el)
        })
        wx.previewImage({
            current: 'https://img.ameimeika.com/' + banners[index], // 当前显示图片的http链接
            urls: arr
        })
    },
    //   微信登录
    mml_login () {
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo'] === true) {
                    // 已经授权，不会弹框
                    // 成功授权
                    if (wx.getStorageSync('user_token')) {
                        // 登陆了
                        that.url_path = 'mine'
                    } else {
                        // 没登陆
                        wx.showLoading({
                            title: '加载中', //提示的内容,
                            mask: true, //显示透明蒙层，防止触摸穿透,
                            success: res => { }
                        })
                        wx.login({
                            success: result => {
                                common.fly_post(
                                    'api/v4_5/minapp/login',
                                    {
                                        code: result.code,
                                        iv: e.mp.detail.iv,
                                        encryptedData: e.mp.detail.encryptedData,
                                        avatarUrl: e.mp.detail.userInfo.avatarUrl,
                                        nickName: e.mp.detail.userInfo.nickName,
                                        sid: this.sid
                                    },
                                    response => {
                                        wx.hideLoading()
                                        let res = response.data
                                        if (res.status_code == 0) {
                                            wx.setStorageSync('user_token', res.data.token)
                                            wx.setStorageSync('user_id', res.data.user_id)
                                            wx.setStorageSync('open_id', res.data.user_jwt)
                                            wx.removeStorage({
                                                key: 'pop_obj',
                                                success: res => { }
                                            })
                                            that.url_path = 'mine'
                                        } else {
                                            wx.hideLoading()
                                            wx.showToast({
                                                title: res.message,
                                                icon: 'none',
                                                duration: 2000,
                                                mask: false
                                            })
                                        }
                                    }
                                )
                            },
                            fail: () => {
                                wx.showToast({
                                    title: '登录失败', //提示的内容,
                                    icon: 'none', //图标,
                                    duration: 1500, //延迟时间,
                                    mask: true, //显示透明蒙层，防止触摸穿透,
                                    success: res => { }
                                })
                            }
                        })
                    }
                } else if (res.authSetting['scope.userInfo'] === false) {
                    // 授权弹窗被拒绝
                    wx.showToast({
                        title: '授权被禁用，请前往设置', //提示的内容,
                        icon: 'none', //图标,
                        duration: 2000, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                        success: res => {
                            wx.openSetting({
                                success: res => { },
                                fail: res => { }
                            })
                        }
                    })
                } else {
                    // 没有弹出过授权弹窗
                    wx.authorize({
                        scope: 'scope.userInfo',
                        success: res => {
                            that.islogin = true
                            that.url_path = 'mine'
                        },
                        fail: () => { },
                        complete: () => { }
                    })
                }
            }
        })
    },
    // 跳转合力客服

   
  
}

export default common


